let id = location.search.split('?')[1];
console.log(id);
if(id){
    $.get(`http://localhost:8000/produce/${id}`,data=>{
        console.log(data);
        $('[name=id]').val(data.id);
        $('[name=name]').val(data.name);
        $('[name=price]').val(data.price);
        $('[name=stock]').val(data.stock);
    })
}


function save(){
    let name =  $('[name=name]').val();
    let price =  $('[name=price]').val();
    let stock =  $('[name=stock]').val();
    let obj = {
        name,
        price,
        stock
    }
    if($('[name=id]').val()){
        obj.id = $('[name=id]').val();
        $.ajax({
            url:'http://localhost:8000/produce',
            type:'put',
            data:obj,
            success:res=>{
               window.location.href='index.html'
            }
        })
    }else{
        $.post('http://localhost:8000/produce',obj,res=>{
            console.log(res);
            window.location.href='index.html'
        })
    }
}